package edu.gatech.app4args;

import edu.gatech.app4args.resources.Globals;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;

import org.netbeans.lib.awtextra.AbsoluteConstraints;
import org.netbeans.lib.awtextra.AbsoluteLayout;

/**
 * Controls the attributes for a matching SlideContentSection.
 *
 * @author Andrew Lunsford
 */
public class SlideInfoSection extends JPanel{
	/** The label for the name of the section */
	private JLabel label;
	/** Allows enabling of liststyle radio buttons */
	private JCheckBox cbListstyle;
	/** Contains the liststyle radio buttons */
	private ButtonGroup lsBGroup;
	/** The radio for bulleted lists */
	private JRadioButton lsBullet;
	/** The radio for numbered lists */
	private JRadioButton lsNumber;
	/** A label for smartart tagging */
	private JLabel smartArtLabel;
	/** A dropdown that allows selection of smartart tags */
	private JComboBox smartArtDropdown;
	/** Used to separate this section from the next */
	private JSeparator separator;

	/** Reference to the parent panel */
	private static SlideInfoPanel slideInfoPanel;
	/** The section type */
	private int section_type;

	/**
	 * Creates a new SlideInfoSection. Sets a reference to the parent panel
	 * and the section type. Sets up all swing objects, adding actionlisteners
	 * and adding them to the section layout.
	 *
	 * @param sip the parent panel
	 * @param sectionType the section type
	 */
	public SlideInfoSection(SlideInfoPanel sip, int sectionType){
		slideInfoPanel = sip;

		this.setName("SIS "+sectionType);
		this.section_type = sectionType;
		this.setLayout(new AbsoluteLayout());


		label = new JLabel();
		label.setFont(new Font("Tahoma", Font.BOLD, 11));
		label.setForeground(Color.black);

		cbListstyle = new JCheckBox();
		cbListstyle.setName("SIS "+sectionType+" List Style Checkbox");
		cbListstyle.setText("List Syle");
        cbListstyle.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
				enableListStyles(cbListstyle.isSelected());
				if(!cbListstyle.isSelected())
					lsBGroup.clearSelection();
					updateListStyle(Globals.lStyle_none);
            }
        });

		lsBGroup = new ButtonGroup();

		lsBullet = new JRadioButton();
		lsBullet.setName("SIS "+sectionType+" Bullet Radio Button");
		lsBullet.setText("Bulleted");
		lsBullet.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
				updateListStyle(Globals.lStyle_bullet);
            }
        });
		lsBGroup.add(lsBullet);

		lsNumber = new JRadioButton();
		lsNumber.setName("SIS "+sectionType+" Number Radio Button");
		lsNumber.setText("Number");
		lsNumber.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
				updateListStyle(Globals.lStyle_number);
            }
        });
		lsBGroup.add(lsNumber);

		this.enableListStyles(false);

		// TODO change to dropdown
		// TODO add button to launch SmartArt help
		smartArtLabel = new JLabel();
		smartArtLabel.setName("SIS "+sectionType+" SmartArt Checkbox");
		smartArtLabel.setText("SmartArt Tag:");

		String[] smartArtStyles = {"None","Process","Hierarchy","Pyramid","Cycle"};
		smartArtDropdown = new JComboBox(smartArtStyles);
		smartArtDropdown.setSelectedIndex(0);
		smartArtDropdown.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
				updateSmartArt();
            }
        });

		separator = new JSeparator();
		separator.setName("SIS "+sectionType+" Separator");

		this.add(label, new AbsoluteConstraints(10, 10, -1, -1));
		this.add(cbListstyle, new AbsoluteConstraints(15, 25, -1, -1));
		this.add(lsBullet, new AbsoluteConstraints(30, 50, -1, -1));
		this.add(lsNumber, new AbsoluteConstraints(150, 50, -1, -1));
		this.add(smartArtLabel, new AbsoluteConstraints(25, 80, -1, -1));
		this.add(smartArtDropdown, new AbsoluteConstraints(125, 75, -1, -1));
		this.add(separator, new AbsoluteConstraints(10, 100, 260,-1));
		this.setMaximumSize(new Dimension(280,110));

		this.enableSection(false);
	}

	/**
	 * Used to change the name of the section.
	 *
	 * @param name the name of the section
	 */
	public void setup(String name){
		label.setText(name);
	}
	
	/**
	 * Enables/disables all swing objects in the section.
	 *
	 * @param enable true to disable, false to disable
	 */
	public final void enableSection(boolean enable){
		label.setEnabled(enable);
		cbListstyle.setEnabled(enable);
		this.enableListStyles(false);
		smartArtLabel.setEnabled(enable);
		separator.setEnabled(enable);
	}

	/**
	 * Used to enable/disable the liststyle radio buttons when the checkbox
	 * is selected.
	 *
	 * @param enable true to enable, false to disable
	 */
	private void enableListStyles(boolean enable){
		lsBullet.setEnabled(enable);
		lsNumber.setEnabled(enable);
	}

	/**
	 * Resets the liststyle button group, and sets the checkbox and radios
	 * according to the liststyle.
	 *
	 * @param liststyle the liststyle to set
	 */
	public void setListStyle(int liststyle){
		lsBGroup.clearSelection();
		if(liststyle==0){
			cbListstyle.setSelected(false);
			this.enableListStyles(false);
		}else{
			cbListstyle.setSelected(true);
			this.enableListStyles(true);
			if(liststyle==Globals.lStyle_bullet)
				lsBullet.setSelected(true);
			else
				lsNumber.setSelected(true);
		}
	}

	public JCheckBox getCbListstyle() {
		return cbListstyle;
	}
	/**
	 * Calls the parent panel to update the liststyle for the current slide.
	 *
	 * @param lStyle the selected liststyle
	 */
	private void updateListStyle(int lStyle){
		slideInfoPanel.updateListStyle(section_type, lStyle);
	}

	/**
	 * Loads the selected smartart style for the current slide.
	 *
	 * @param saStyle the selected smartart style
	 */
	public void setSmartArt(int saStyle){
		smartArtDropdown.setSelectedIndex(saStyle);
	}

	/**
	 * Calls the parent panel to update the smartart for the current slide.
	 */
	private void updateSmartArt(){
		slideInfoPanel.updateSmartArt(section_type, smartArtDropdown.getSelectedIndex());
	}

        public JLabel getLabel() {
            return label;
        }
}
